package org.ithang.lock;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RedisLockTest {

	private int count=100;
	
	private RedisLock lock=null;
	@Before
	public void init(){
		lock=new RedisLock();
	}
	
	
	@Test
	public void ticketTest(){
		
		new Thread(()->sell(),"1号售票员").start();
		new Thread(()->sell(),"2号售票员").start();
		new Thread(()->sell(),"3号售票员").start();
		
		try{
		    Thread.sleep(50000);
		}catch(Exception e){}
	}
	
	
	private void sell(){
				lock.lock();
				while(count>0){		
					try{
						 System.out.println(Thread.currentThread().getName()+" 卖出第"+count+"张票");
					        count--;
					        Thread.sleep(800);
					}catch(Exception e){
						
					}finally{
						lock.unlock();
					}
			       
				}
			
	}
	
	
	
	@After
	public void destory(){
		try{
			Thread.sleep(50000);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
}
